package com.hunk.core.json.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hunk.core.auth.entity.User;
import com.hunk.core.json.base.JsonRequest;
import com.hunk.core.json.base.JsonResponse;
import com.hunk.utils.JacksonUtil;
import com.hunk.utils.io.Print;

@Controller
@RequestMapping("test/json/controller/")
public class LoginController {
	
//	@ResponseBody
//	@RequestMapping(value="/login", method=RequestMethod.POST)
//	public JsonResponse<?> login(HttpEntity<String> httpEntity) {
//		Print.print(httpEntity.getBody());
//		
//		JsonRequest<User> jreq = JacksonUtil.readJsonFromString(httpEntity.getBody(), new TypeReference<JsonRequest<User>>(){});
//		JsonResponse<User> resp = new JsonResponse<User>("登录失败!");
//		Print.println(jreq.getReq().getClass().getSimpleName());
//		
//		User u = jreq.getReq();
//		User user = new User("hunk", "123", "123");
//		if (user != null && StringUtils.equals(user.getPassword(), u.getPassword())) {
//			resp.setResult(true);
//			resp.setResp(user);
//			resp.setError("登录成功");
//		}
//		return resp;
//	}
	
	@ResponseBody
	@RequestMapping(value="/login", method=RequestMethod.POST)
	public JsonResponse<?> login(@RequestBody JsonRequest<User> jreq) {
		JsonResponse<User> resp = new JsonResponse<User>("登录失败!");
		Print.println(jreq.getReq());
		
		User u = jreq.getReq();
		User user = new User("hunk", "123", "123");
		if (user != null && StringUtils.equals(user.getPassword(), u.getPassword())) {
			resp.setResult(true);
			resp.setResp(user);
			resp.setError("登录成功");
		}
		return resp;
	}
	
	public static void main(String[] args) {
		JsonRequest<User> jr = new JsonRequest<User>("long", new User("123", "controller", "controller"));
		Print.println(JacksonUtil.getJsonStrFromBean(jr));
	}
}
